using System;
using System.Collections.Generic;
using System.Text;
using Umdf.SideShow.Events;
using Umdf.SideShow.Contents;
using System.IO;

namespace Umdf.SideShow.PocketPC
{
    public class PocketPCClientChannel : PocketPCChannelBase, ICallbackChannel
    {
        private IPocketPCChannel _channel;

        public PocketPCClientChannel(IPocketPCChannel channel)
        {
            _channel = channel;
        }

        protected override void OnDataReceived(byte[] data)
        {
            using (MemoryStream ms = new MemoryStream(data))
            {
                using (BinaryReader reader = new BinaryReader(ms))
                {
                    while (ms.Position < ms.Length)
                    {
                        Guid cmd = new Guid(reader.ReadBytes(16));
                        if (cmd == ChannelMessages.AddApplication)
                        {
                            SideShowApplication app = new SideShowApplication(reader);
                            _channel.AddApplication(app);
                        }
                        else if (cmd == ChannelMessages.RemoveApplication)
                        {
                            Guid id = new Guid(reader.ReadBytes(16));
                            _channel.RemoveApplication(id);
                        }
                        else if (cmd == ChannelMessages.AddContent)
                        {
                            Guid appId = new Guid(reader.ReadBytes(16));
                            Guid epId = new Guid(reader.ReadBytes(16));
                            string typeName = reader.ReadString();
                            Type t = Type.GetType(typeName);
                            Content c = (Content)Activator.CreateInstance(t);
                            c.DeserializeObject(reader);
                            _channel.AddContent(appId, epId, c);
                        }
                        else if (cmd == ChannelMessages.RemoveContent)
                        {
                            Guid appId = new Guid(reader.ReadBytes(16));
                            Guid epId = new Guid(reader.ReadBytes(16));
                            uint cntId = reader.ReadUInt32();
                            _channel.RemoveContent(appId, epId, cntId);
                        }
                        else if (cmd == ChannelMessages.Initialize)
                        {
                            _channel.Initialize();
                        }
                        else if (cmd == ChannelMessages.Deinitialize)
                        {
                            _channel.Deinitialize();
                        }
                    }
                }
            }
        }

                
        #region IClientChannel Members

        public void PostEvent(Umdf.SideShow.Events.SideShowEvent e)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(ms))
                {
                    writer.Write(ChannelMessages.PostEvent.ToByteArray());
                    writer.Write(e.GetType().FullName);
                    e.SerializeObject(writer);
                    Send(ms.ToArray());
                }
            }
        }

        #endregion
    }
}
